import json
import httpclient as http
import strutils
import sequtils
import asyncdispatch as ad

export json
export ad

const token = "244736763:AAGTVQ0kbIqU0M4rsBrMYhdnhfyF7CTi53k"
const api_prefix = "https://api.telegram.org/bot" & token & "/"

proc PascalCase_to_camelCase(str: string): auto {.compiletime.} =
  result = str
  result[0] = result[0].toLower

proc dash_to_PascalCase(str: string): auto {.compiletime.} =
  str.to_lower
     .split('-')
     .map(proc(x: auto): auto = x.capitalize)
     .join

proc dash_to_camelCase(str: string): auto {.compiletime.} =
    str.dash_to_PascalCase
       .PascalCase_to_camelCase

proc api_address(method_name: string): auto {.compiletime.} =
    api_prefix & method_name.dash_to_camelCase

type API_argument* = tuple[name, content: string]

proc call_api_impl(address: string, args: seq[API_argument]): Future[Json_node] {.async.} =
    let mpdata  = args.new_multipart_data
    var client  = new_async_http_client()
    let resp    = await client.post(address, multipart=mpdata)
    return parse_json(resp.body)

template call_api*(method_name: string, args: seq[API_argument]): Future[Json_node] =
    const address = api_address(method_name)
    call_api_impl(address, args)

